package de.voegel.controller.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class PoolFactory {

	public static final String USER = "scott";
	public static final String PW = "tiger";

	private static String P_USER;
	private static String P_PW;

	// FIXME Ich bin noch kein Pool
	private static Connection c;

	public static final Connection getConnection() throws SQLException {
		if (c == null) {
			if (P_USER == null && P_PW == null) {
				initConnection();
			} else {
				initConnection(P_USER, P_PW);
			}
		}
		return c;
	}

	public static final Connection getConnection(String username,
			String password) throws SQLException {
		P_USER = username;
		P_PW = password;
		if (c == null) {
			initConnection(P_USER, P_PW);
		}
		if (c == null) {
			throw new SQLException("DB-User unknown");
		}
		return c;
	}

	public static void closeConnection() throws SQLException {
		if (c != null) {
			c.close();
			c = null;
		}
	}

	private static void initConnection() {
		initConnection(USER, PW);
	}

	private static void initConnection(String username, String password) {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url = "jdbc:oracle:thin:@oracle.informatik.haw-hamburg.de:1521:inf09";
			c = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
